<?php

class SearchController extends Controller
{
	public function actionIndex($title = null, $ticket = null, $category = null, $start = null, $end = null, $location = null)
	{
		if (isset($_POST['SearchForm'])) {
			$ticket = $_POST['SearchForm']['ticket'];
			$title = $_POST['SearchForm']['title'];
            $category = $_POST['SearchForm']['category'];
            $start = $_POST['SearchForm']['start'];
            $end = $_POST['SearchForm']['end'];
            $location = $_POST['SearchForm']['location'];
		}
		
		$criteria = new CDbCriteria();
        $criteria->with=array('location', 'category');
        $criteria->compare('t.title', trim($title), true);
		if ($ticket != null) {
			$criteria->addCondition('t.status="A" AND t.ticket="' . $ticket . '" AND t.enddate >= NOW()');
		}
		else {
			$criteria->addCondition('t.status="A" AND t.enddate >= NOW()');
		}
        if ($start != null) {
            $criteria->addCondition('t.startdate >= "' . $start . ' 0:00:00' . '"');
        }
        if ($end != null) {
            $criteria->addCondition('t.enddate <= "' . $end . ' 23:59:59' . '"');
        }
        $criteria->compare('location.title', $location, true);
        $criteria->compare('category.id', $category);
        
		$dataProvider=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'updatedate DESC, enddate ASC, startdate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		if (isset($_POST['SearchForm'])) {
			$this->redirect(array('/Events/search/index',
                'title'=>$title,
                'ticket'=>$ticket,
                'category'=>$category,
                'start'=>$start,
                'end'=>$end,
                'location'=>$location
            ));
		}
		
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
			'title'=>$title
		));
	}
}